﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BASE;
using DevExpress.Web.ASPxGridView;
using System.Data.OleDb;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Text;

public partial class Default2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        
        if (!IsPostBack)
        {
            PopulateData();
        }
    }

    private void PopulateData()
    {
        using (SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["conStr"].ConnectionString))
        {
            SqlCommand cmd = new SqlCommand("Select top 10 * from sinhvien", con);
            if (con.State != System.Data.ConnectionState.Open)
            {
                con.Open();
            }
            DataTable dt = new DataTable();
            dt.Load(cmd.ExecuteReader());
            gvContacts.DataSource = dt;
            gvContacts.DataBind();
            con.Close();
        }
    }

    protected void btnAddRow_Click(object sender, EventArgs e)
    {
        AddRowsToGrid();
    }
    //and here is the function 

    private void AddRowsToGrid()
    {
        List<entSinhVien> lst = new List<entSinhVien>();
        entSinhVien entSV = new entSinhVien();
        // Check Validity
        TextBox txtFirstName;
        TextBox txtLastName;
        TextBox txtContactNo;

        foreach (GridViewRow row in gvContacts.Rows)
        {
            entSV = new entSinhVien();
            txtFirstName = (TextBox)row.FindControl("txtFirstName");
            txtLastName = (TextBox)row.FindControl("txtLastName");
            txtContactNo = (TextBox)row.FindControl("txtContactNo");
            entSV.MaSinhVien = ((TextBox)row.FindControl("txtFirstName")).Text;
            entSV.Ho = txtLastName.Text;
            entSV.Ten = txtContactNo.Text;
            lst.Add(entSV);
        }
        entSV = new entSinhVien();
        lst.Add(entSV);


        gvContacts.DataSource = lst;
        gvContacts.DataBind();
       
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        // Save Here
        StringBuilder sb = new StringBuilder();
        sb.AppendLine("<?xml version=\"1.0\" ?>");
        sb.AppendLine("     <contacts>");

        // Check Validity
        TextBox txtFirstName;
        TextBox txtLastName;
        TextBox txtContactNo;

        foreach (GridViewRow row in gvContacts.Rows)
        {
            txtFirstName = (TextBox)row.FindControl("txtFirstName");
            txtLastName = (TextBox)row.FindControl("txtLastName");
            txtContactNo = (TextBox)row.FindControl("txtContactNo");

            if (txtFirstName == null || txtLastName == null || txtContactNo == null)
            {
                return;
            }

            if (string.IsNullOrEmpty(txtFirstName.Text.Trim())
                || string.IsNullOrEmpty(txtLastName.Text.Trim())
                || string.IsNullOrEmpty(txtContactNo.Text.Trim()))
            {
                lblMsg.Text = "All fields are required!";
                return;
            }
            else
            {
                sb.AppendLine("     <contact>");
                sb.AppendLine("         <FirstName>" + txtFirstName.Text.Trim() + "</FirstName>");
                sb.AppendLine("         <LastName>" + txtLastName.Text.Trim() + "</LastName>");
                sb.AppendLine("         <ContactNo>" + txtContactNo.Text.Trim() + "</ContactNo>");
                sb.AppendLine("     </contact>");
            }

        }
        sb.AppendLine("     </contacts>");

        using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["con"].ConnectionString))
        {
            SqlCommand cmd = new SqlCommand("ContactBulkInsert", con);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@XMLData", sb.ToString());
            if (con.State != ConnectionState.Open)
            {
                con.Open();
            }

            int affRow = cmd.ExecuteNonQuery();
            if (affRow > 0)
            {
                lblMsg.Text = "Successfully " + affRow + " record inserted.";
                PopulateData();
                AddRowsToGrid();
            }
        }
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        
    }
}